package Calculation;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Hua YY
 * Date: 2024-09-28
 * Time: 20:44
 */
public class Test2 {
    //双指针0复写
    class Solution {
        public void duplicateZeros(int[] arr) {
            int cur = 0 , dest = -1 , n = arr.length;
            while(cur <= n){//dest位置不确定所以不能用作判断循环的条件
                if(arr[cur] != 0){
                    dest++;
                }else{
                    dest += 2;
                }
                if(dest >= n-1){
                    break;
                }
                cur++;
            }
            if(dest == n){
                arr[n-1] = 0;
                dest -= 2;
                cur--;
            }

            //开始从后往前复写
            while(cur >= 0 ){
                if(arr[cur] != 0){
                    arr[dest] = arr[cur];
                    cur--;
                    dest--;
                }else{
                    arr[dest] = arr[cur];
                    dest--;
                    arr[dest] = arr[cur];
                    cur--;
                    dest--;
                }
            }
        }
    }
}
